package com.example.myshixun;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myshixun.Bear.UserBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    EditText editTextUserName;
    EditText editTextPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

        editTextUserName = findViewById(R.id.userName);
        editTextPassword = findViewById(R.id.password);

        Button buttondenglu =(Button) findViewById(R.id.denglu);
        buttondenglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editTextUserName.getText().toString()) ||
                        TextUtils.isEmpty(editTextPassword.getText().toString())) {
                    Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }
            }
        });
        Button zhuce = findViewById(R.id.zhuce);
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent();
                it.setClass(MainActivity.this,MyShixun2.class);
                MainActivity.this.startActivity(it);
            }
        });

    }

    private void login() {
        final String userName = editTextUserName.getText().toString();
        final String password = editTextPassword.getText().toString();
        new Thread(new Runnable() {
//            private static final String TAG="login";
            @Override
            public void run() {

                OkHttpClient client = new OkHttpClient();
                String url = "http://10.20.64.6:8080/web_war_exploded/All/user?telephone="+editTextUserName.getText().toString();
                FormBody body = new FormBody.Builder().build();
                Request request = new Request.Builder().url(url).post(body).build();
                final Call call = client.newCall(request);
                try {

                    Response response = call.execute();
                    final String data = response.body().string();
                    Gson gson = new Gson();
                    Log.e("TAG", "run: 开始登录" );
                    final UserBean userBean = gson.fromJson(data,UserBean.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (data.length()==4||!editTextPassword.getText().toString().equals(userBean.getPassword())){
                                Toast.makeText(MainActivity.this,"用户名或密码错",Toast.LENGTH_SHORT).show();
                            }else {
                                Intent intent = new Intent(MainActivity.this,MyShixun3.class);
                                startActivity(intent);
                            }
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}